package com.shxc.smarttrade.old.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.activity.SupermanDetailActivity;
import com.shxc.smarttrade.old.activity.SupermanListTwoActivity;
import com.shxc.smarttrade.old.adapter.MasterListTwoAdapter;
import com.shxc.smarttrade.old.bean.Master;
import com.shxc.smarttrade.old.constants.URLContents;
import com.shxc.smarttrade.old.myview.MyFootView;
import com.shxc.smarttrade.old.myview.MyRefreshHeadView;
import com.shxc.smarttrade.old.myview.RecycleViewDivider;
import com.shxc.smarttrade.old.utils.MyOkHttpUtils;
import com.shxc.smarttrade.old.utils.SpUtils;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by jcdev1 on 2017/10/10.
 */

public class MasterEarningFragment extends Fragment {

    @BindView(R.id.tv_explain_text)
    TextView tvExplainText;
    @BindView(R.id.ll_nodata_bg)
    AutoLinearLayout llNodataBg;
    @BindView(R.id.x_recycler_view)
    XRecyclerView xRecyclerView;

    private View rootView;
    private LinearLayoutManager manager;
    private int page = 1;
    private MasterListTwoAdapter adapter;
    private List<Master.DataBean.MastersBean> allMasterList = new ArrayList<>();
    private SupermanListTwoActivity parentActivity;


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Master master = (Master) msg.obj;
                    if (master.getErr_code().equals("0")) {
                        List<Master.DataBean.MastersBean> mastersBeanList = master.getData().getMasters();
                        if (mastersBeanList.size() != 0) {
                            allMasterList.addAll(mastersBeanList);
                            adapter.setDatas(allMasterList);
                        } else {
                            if (xRecyclerView != null)
                                xRecyclerView.setNoMore(true);
                            if (allMasterList.size() != 0)
                                parentActivity.showToast("已加载全部！");
                        }
                    } else {
                        parentActivity.showToast(master.getErr_msg());
                    }
                    if (allMasterList.size() == 0)
                        xRecyclerView.setEmptyView(llNodataBg);
                    if (xRecyclerView != null)
                        xRecyclerView.refreshComplete();

                    break;
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null)
            rootView = inflater.inflate(R.layout.fragment_super_man, null);
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null)
            parent.removeView(rootView);
        ButterKnife.bind(this, rootView);
        initView();
        initListener();
        initData(page, 10);
        return rootView;
    }

    private void initView() {
        parentActivity = (SupermanListTwoActivity) getActivity();
        manager = new LinearLayoutManager(getContext());
        xRecyclerView.setLayoutManager(manager);
        xRecyclerView.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayout.VERTICAL, 20, getResources().getColor(R.color.all_background)));
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);
        MyRefreshHeadView myRefreshHeadView = new MyRefreshHeadView(getContext());
        myRefreshHeadView.setArrowImageView(R.mipmap.indicator_arrow);
        xRecyclerView.setRefreshHeader(myRefreshHeadView);
        xRecyclerView.setFootView(new MyFootView(getContext()));
        xRecyclerView.setAdapter(adapter = new MasterListTwoAdapter(getContext()));
    }

    private void initListener() {
        adapter.setMyItemCLickListener(new MasterListTwoAdapter.MasterTwoItemClickListener() {
            @Override
            public void ItemViewClickListener(Master.DataBean.MastersBean mb, int posintion) {
                Intent intent = new Intent(getActivity(), SupermanDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("masterSBean", mb);
                bundle.putInt("where", 2);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        xRecyclerView.refreshComplete();
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        initData(page, 10);
                    }
                }, 1000);

            }
        });
    }

    private void initData(int page, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", SpUtils.getString(getActivity(), "accessToken", ""));
        map.put("order", "1");
        map.put("page", page + "");
        map.put("pageSize", pageSize + "");
        MyOkHttpUtils.getServiceDatas(getActivity(),map, URLContents.SUPER_MAN_LIST, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                parentActivity.showToast("网络异常");
            }

            @Override
            public void success(String response, int id) {
                Gson gson = new Gson();
                Master master = gson.fromJson(response, Master.class);
                Message msg = Message.obtain();
                msg.what = 0;
                msg.obj = master;
                handler.sendMessage(msg);
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
